
#include "ModulsManager\ClassAINmodul.h"


const AINsets AINdflt = {
                          4,//Nmean
                          0x3F,//UtoI
                          {1000,1000,1000,1000,1000,1000,1000,1000},//ChPwrDelay[]
                          0x00,//ChPwrType
                          0x00,//ChMeas
                          0x00,//ChPwrMod1
                          0x00,//ChPwrMod2
                          0//MeasDelay
                         };



/****************************************************************************************************/
/*                           A I N _ M O D U L E      C O N S T R U C T O R                         */
/****************************************************************************************************/
AINmodul::AINmodul(uint8_t devID, uint8_t modKey, AinData *pResult) : BASEmodul(devID,modKey)
{
  pData = pResult; pAINsets = &AINdflt;
}


AINmodul::AINmodul(uint8_t devID, uint8_t modKey, AinData *pResult, AINsets const *pSets) : BASEmodul(devID,modKey)
{
  pData = pResult;  pAINsets = pSets;
}




STATUS_MODUL AINmodul::ReadCHvals(void)
{
  uint32_t ch, nChMax;    STATUS_MODUL status;
//
  status = RdMod(aADCval,NofChMax<<2);
  nChMax = NofCh<NofChMax ?   NofCh : NofChMax;
//  
  if(status==ALL_RIGHT)
   { 
     for(ch=0; ch<nChMax; ++ch)
      {  pData->AIN_Val[ch] = BufToUINT(ch<<2);  }
      while(ch<NofChMax)  {pData->AIN_Val[ch++] = 0;}
   }
//
  return status;
}



STATUS_MODUL AINmodul::WriteStart(uint8_t StartTyp, uint8_t ChNumb)
{
  StartTyp |= ChNumb&0x0F;  return WrMod(aMeasStrt,1,&StartTyp);
}



void AINmodul::SetUpModState(void)
{
  pData->Pwr_State = BufToUSHT(aChPwr-aCR1st);
  pData->ChSC_State = BufToUSHT(aChSC-aCR1st);
  pData->MErr_State = BufToUSHT(aMeasErr-aCR1st);
}





STATUS_MODUL AINmodul::SetUpModul(void)
{
  STATUS_MODUL status;
//
  status = ReadModInfo(MinModTypeAIN);
  if(status==ALL_RIGHT)
    { pData->NofCH = NofCh;  status = WriteAINsets(); }
  if(status==ALL_RIGHT)
    { /*WrCR1(WrFALSH);*/  WrKey(); }
//
  return status;
}



STATUS_MODUL AINmodul::WriteAINsets(void)
{
  STATUS_MODUL status;
//
//  pAINsets->UtoI &= CHmask;
//  pAINsets->ChPwrType &= CHmask;  pAINsets->ChMeas &= CHmask;
//  pAINsets->ChPwrMod1 &= CHmask;  pAINsets->ChPwrMod2 &= CHmask;
//
  status = WrMod(aNmean,1,&pAINsets->Nmean);
  if(status==ALL_RIGHT) status = WrMod(aUtoI,1,&pAINsets->UtoI);
  if(status==ALL_RIGHT) status = WrMod(aDelay,NofCh<<1,&pAINsets->ChPwrDelay);
  if(status==ALL_RIGHT) status = WrMod(aChPwrTyp,2,&pAINsets->ChPwrType);
  if(status==ALL_RIGHT) status = WrMod(aChEn,2,&pAINsets->ChMeas);
  if(status==ALL_RIGHT) status = WrMod(aPwrMod1,2,&pAINsets->ChPwrMod1);
  if(status==ALL_RIGHT) status = WrMod(aPwrMod2,2,&pAINsets->ChPwrMod2);
  if(status==ALL_RIGHT) status = WrMod(aMeasBreak,2,&pAINsets->MeasDelay);
//
  return status;
}


bool AINmodul::CheckMeasEnd(portTickType TimeOut_mS)
{
  portTickType Time, TimeX;  bool TimeCheck;
  uint32_t dCikl;
//
  Time = xTaskGetTickCount();   TimeX = Time + TimeOut_mS/portTICK_RATE_MS;
  if(TimeX<20/portTICK_RATE_MS) TimeX += 20/portTICK_RATE_MS;
  if(TimeX<Time) {TimeCheck = false;} else {TimeCheck = true;}
//  
  while(1)
   { 
     ReadStatus();
     if(CR1St&fDATArdy) return true;
     if(TimeCheck)
       { if(TimeX<=xTaskGetTickCount()) return false; }
      else
       { if(TimeX>=xTaskGetTickCount()) TimeCheck = true; }
     for(dCikl=100000; dCikl; --dCikl);
     //vTaskDelay(20/portTICK_RATE_MS);
   }
//  
}





